package com.zhuxiang.server;

import com.zhuxiang.model.Question;
import com.zhuxiang.model.Student;
import com.zhuxiang.model.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 编程实现数据的存取
 */
public class ServerDao {

    private List list;
    private ObjectInputStream ois;
    private ObjectOutputStream oos;

    public ServerDao() {
    }

    public ServerDao(List list) {
        this.list = list;
    }

    /**
     * 编程实现管理员账号密码的校验
     * @param user
     * @return
     */
    public boolean serverManagerCheck(User user){
        if("admin".equals(user.getUserName()) && "123456".equals(user.getPassword())){
            return true;
        }
        return false;
    }

    /**
     * 编程实现学员账号密码的校验
     * @param user
     * @return
     */
    public int serverUserCheck(User user){
        List<Student> list = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        String userName = user.getUserName();
        String password = user.getPassword();
        try {
            ois = new ObjectInputStream(new FileInputStream("student.txt"));
            list = (List<Student>) ois.readObject();
            for (int i = 0;i < list.size();i++){
                nameList.add(list.get(i).getUserName());
            }
            int index = nameList.indexOf(userName);
            if (index != -1 ){
                if (list.get(index).getPassword().equals(password)){
                    return 1;
                }else {
                    return -1;
                }
            }else {
                return 0;
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       return -1;
    }

    public List  readStu() throws IOException, ClassNotFoundException {
        File file = new File("student.txt");
        if (file.length() > 0){
            ois = new ObjectInputStream(new FileInputStream("student.txt"));
            list = (List<Student>) ois.readObject();
            return list;
        }else {
            writeStu();
        }
        if (ois != null){
            ois.close();
        }
        return null;
    }

    public void writeStu() throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("student.txt"));
        oos.writeObject(list);
        System.out.println(list);
        oos.flush();
        if (oos != null){
            oos.close();
        }
    }

    public List readExam() throws IOException, ClassNotFoundException {
        File file = new File("question.txt");
        if (file.length() > 0){
            ois = new ObjectInputStream(new FileInputStream("question.txt"));
            list = (List<Question>) ois.readObject();
            return list;
        }else {
            writeExam();
        }
        if (ois != null){
            ois.close();
        }
        return null;
    }
    public void writeExam() throws IOException {
        oos = new ObjectOutputStream(new FileOutputStream("question.txt"));
        oos.writeObject(list);
        System.out.println(list);
        oos.flush();
        if (oos != null){
            oos.close();
        }
    }
}
